package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.mapper.DeviceInformationMapper;
import com.j283_5.wisdom_park.pojo.Device;
import com.j283_5.wisdom_park.pojo.DeviceInformation;
import com.j283_5.wisdom_park.service.DeviceInformationService;
import com.j283_5.wisdom_park.vo.device_management.DeviceInformationVo;
import com.j283_5.wisdom_park.vo.device_management.DeviceVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.interceptor.TransactionalProxy;

import javax.annotation.Resource;
import java.lang.annotation.Documented;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
@Transactional
public class DeviceInformationServiceImpl extends ServiceImpl<DeviceInformationMapper, DeviceInformation> implements DeviceInformationService {

    @Resource
    private DeviceInformationMapper deviceInformationMapper;

    /**
     * 查询所有设备，包括模糊查询
     * @param page
     * @param deviceInformationVo
     * @return
     */
    @Override
    public List<DeviceInformationVo> queryDeviceInformation(Page page,DeviceInformationVo deviceInformationVo,Integer adminId) {
        System.out.println(deviceInformationVo.toString());
        List<DeviceInformation> deviceInformations = deviceInformationMapper.queryDeviceInformation(new DeviceInformation(deviceInformationVo), (int)((page.getCurrent()-1)*page.getSize()), (int)(page.getSize()),adminId);
        List<DeviceInformationVo> deviceInformationVoList = new ArrayList();
        deviceInformations.forEach((v)->{

            deviceInformationVoList.add(new DeviceInformationVo(v));
        });
        return deviceInformationVoList;
    }

    /**
     * 根据条件查询所有设备数量
     * @param deviceInformationVo
     * @return
     */
    @Override
    public Integer queryDeviceCount(DeviceInformationVo deviceInformationVo,Integer adminId) {
        return deviceInformationMapper.queryDeviceSum(new DeviceInformation(deviceInformationVo),adminId);
    }

    /**
     * 根据设备id查询设备所有详细信息
     * @param dinfId
     * @return
     */
    @Override
    public DeviceVo queryOneDevice(Integer dinfId) {
        return new DeviceVo(deviceInformationMapper.queryOneDevice(dinfId));
    }

    /**
     * 通过list多选删除数据
     * @param deviceIdList
     * @return
     */
    @Override
    public boolean deleteDevice(ArrayList deviceIdList) {
        int row = deviceInformationMapper.deleteDevice(deviceIdList);
        if(deviceIdList.size() == row){
            return true;
        }
        //手动事务回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return false;
    }

    /**
     * 添加设备信息
     * @param deviceVo
     * @return
     */
    @Override
    public boolean addDeviceInformation(DeviceVo deviceVo) {
        Device device = new Device(deviceVo);
        if (1 ==  deviceInformationMapper.addDeviceDetailed(device)){
            if(1 == deviceInformationMapper.addDeviceInformation(device,deviceInformationMapper.getDeviceDetailedId())){
                return true;
            }
        }
        return false;
    }
}
